<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('项目名称')" prop="dealId">
            <router-link
              target="_blank"
              :to="{path:'/deal/home/dealBaseHome',query:{id:form.dealId,companyId:form.companyId}}"
              class="link_a"
            >{{ form.dealName }}
            </router-link>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('公司名称')" prop="companyId">
            <router-link
              target="_blank"
              :to="{path:'/company/base/companyBaseHome',query:{companyId:form.companyId}}"
              class="link_a"
            >{{ form.companyName }}
            </router-link>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('申请标题')" prop="actTitle">
            <el-input v-model="form.actTitle" :placeholder="$t('申请标题')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('项目经理')" prop="personCharge">
            <UserSelect
              v-model="form.personCharge"
              :placeholder="$t('项目经理')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('预计投资时间')" prop="dateInvestmentEstimate">
            <el-date-picker
              v-model="form.dateInvestmentEstimate"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('预计投资时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('预计投资金额(万)')" prop="amountInvestmentEstimate">
            <Money v-model="form.amountInvestmentEstimate" :placeholder="$t('预计投资金额')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('公司网址')" prop="companyUrl">
            <el-input v-model="form.companyUrl" :placeholder="$t('公司网址')" show-word-limit maxlength="200" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('项目来源类型')" prop="dealSourceType">
            <dictSelect
              v-model="form.dealSourceType"
              :clearable="true"
              dictionary="deal_source_type"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('项目来源')" prop="dealSource">
            <el-input v-model="form.dealSource" :placeholder="$t('项目来源')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('聚焦行业(一级)')" prop="industryFocus">
            <el-select v-model="form.industryFocus" :placeholder="$t('聚焦行业(一级)')" clearable @change="changeTwo(true)">
              <el-option
                v-for="dict in industryFocusOptions"
                :key="dict.id"
                :label="$t(dict.name)"
                :value="dict.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('聚焦行业(二级)')" prop="industryFocusTwo">
            <el-select v-model="form.industryFocusTwo" :placeholder="$t('聚焦行业(二级)')" clearable>
              <el-option
                v-for="dict in industryFocusTwoOptions"
                :key="dict.id"
                :label="$t(dict.name)"
                :value="dict.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('投资轮次')" prop="round">
            <el-select v-model="form.round" multiple :placeholder="$t('投资轮次')">
              <el-option
                v-for="dict in roundsInvestmentOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('投资类型')" prop="investmentType">
            <dictSelect
              v-model="form.investmentType"
              :clearable="true"
              dictionary="equity_investment_type"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('公司总股数/注册资本(万)')" prop="totalShare">
            <Money v-model="form.totalShare" :placeholder="$t('公司总股数/注册资本')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('投前估值(万)')" prop="valuationBeforeInvestment">
            <Money v-model="form.valuationBeforeInvestment" :placeholder="$t('投前估值')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('是否有预计退出时间')" prop="isExitTime">
            <dictSelect
              v-model="form.isExitTime"
              :clearable="true"
              dictionary="is_exit_time"
            />
          </el-form-item>
        </el-col>
        <el-col v-if="form.isExitTime === 'S'" :span="12">
          <el-form-item :label="$t('预计退出时间')" prop="estimatedExitTime">
            <el-date-picker
              v-model="form.estimatedExitTime"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('预计退出时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('退出计划')" prop="planExit">
            <el-input v-model="form.planExit" type="textarea" :placeholder="$t('退出计划')" show-word-limit maxlength="2000" />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item :label="$t('项目简介')" prop="dealIntroduction">
            <el-input v-model="form.dealIntroduction" type="textarea" :placeholder="$t('项目简介')" show-word-limit maxlength="2000" />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item :label="$t('项目优势')" prop="dealStrongPoint">
            <el-input v-model="form.dealStrongPoint" type="textarea" :placeholder="$t('项目优势')" show-word-limit maxlength="2000" />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item :label="$t('项目缺点/风险')" prop="dealScruple">
            <el-input v-model="form.dealScruple" type="textarea" :placeholder="$t('项目缺点/风险')" show-word-limit maxlength="2000" />
          </el-form-item>
        </el-col>

      </el-row>
      <div class="area-header">
        <span class="area-header-title">{{ $t('投资事项') }}</span>
      </div>
      <el-table
        :data="form.dealWfHxsInvestmentList"
        border
        class="nowrapTable formTable"
      >
        <el-table-column :label="$t('序号')" align="center" width="50">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('主体类型')" align="center">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('主体类型') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'dealWfHxsInvestmentList.' + scope.$index + '.investorType' " :rules="dealTeamRules.investorType">
              <dictSelect
                v-model="scope.row.investorType"
                :clearable="true"
                dictionary="deal_investment_type"
              />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('主体名称')" align="center">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('主体名称') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'dealWfHxsInvestmentList.' + scope.$index + '.investorId' " :rules="dealTeamRules.investorId">
              <MainBodySelect
                v-model="scope.row.investorId"
                :parameters="scope.row.investorType"
                @change="isExist(scope.row.investorId, scope.$index)"
              />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('预计投资金额') + $t('金额单位')" align="center">
          <template slot-scope="scope">
            <el-form-item :prop="'dealWfHxsInvestmentList.' + scope.$index + '.amountInvestmentEstimate' ">
              <Money v-model="scope.row.amountInvestmentEstimate" unit="w" :placeholder="$t('预计投资金额') + $t('金额单位')" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column v-if="!otherBtnHide" :label="$t('操作')" align="center" width="80">
          <template slot-scope="scope">
            <el-button size="small" type="text" icon="el-icon-delete" @click="delHandle(scope.$index)" />
          </template>
        </el-table-column>
      </el-table>
      <div v-if="!otherBtnHide" class="tol">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="addHandle">{{ $t("添加") }}</el-button>
      </div>
      <FileTable :list.sync="form.fileList" data-type="deal_hxs_process" :btn-show="!otherBtnHide" />
    </el-form>
  </div>
</template>

<script>
    import { industryList } from '@/api/sass/hxs/dealHxsWfStorage'
    import { getDealHxsWfProtocol, saveDealHxsWfProtocol, getDealInitialize } from '@/api/sass/hxs/dealHxsWfProtocol'
    import MainBodySelect from '@/components/MainBodySelect/index'
    export default {
      components: {
        MainBodySelect
      },
        data() {
            return {
              roundsInvestmentOptions: [],
              title: '协议评审会',
              // 是否离开选项
              leaveOptions: [],
              // 聚焦行业一级选项
              industryFocusOptions: [],
              // 聚焦行业二级选项
              industryFocusTwoOptions: [],
              // 是否隐藏其它按钮
              otherBtnHide: false,
                // 表单参数
                form: {
                  dealWfHxsInvestmentList: [],
                  fileList: []
                },
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                  actTitle: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ],
                  personCharge: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ],
                  dealSourceType: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ]
                },
              dealTeamRules: {
                investorType: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                investorId: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
              }
            }
        },
        created() {
          this.$bus.$emit('async_done')
          const row = {
            parentId: '0'
          }
          // 一级字典
          industryList(row).then(response => {
            this.industryFocusOptions = response.data
          })
          this.getDicts('sys_yes_no').then(response => {
            this.leaveOptions = response.data
          })
          // 投资轮次
          this.getDicts('rounds_investment').then(response => {
            this.roundsInvestmentOptions = response.data
          })
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                  getDealHxsWfProtocol(data).then(response => {
                        this.form = response.data
                    if (this.form.industryFocus) {
                      this.changeTwo(false)
                    }
                        this.loading = false
                    }).catch(() => {
                      this.loading = false
                    })
                } else {
                    this.reset()
                  getDealInitialize(this.$route.query.id).then(response => {
                    this.form = response.data
                    if (this.form.industryFocus) {
                      this.changeTwo(false)
                    }
                    if (data != null) {
                      this.form.fileList = data.fileList
                    }
                      this.loading = false
                  })
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                  dealId: this.$route.query.id,
                  companyId: this.$route.query.companyId,
                  actTitle: undefined,
                  dealName: undefined,
                  dealIntroduction: undefined,
                    delFlag: undefined,
                    createTime: undefined,
                    createBy: undefined,
                    updateTime: undefined,
                    updateBy: undefined,
                  dealWfHxsInvestmentList: [],
                  fileList: []
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
          editForm() {
            this.formOpen = true
            this.$nextTick(() => {
              this.$refs.dealBaseForm.openLocation = 'bfHome'
              this.$refs.dealBaseForm.init(this.$route.query)
            })
          },
          /** 提交按钮 */
          submitForm: function() {
            return new Promise((resolve, reject) => {
              this.$refs['form'].validate(valid => {
                if (valid) {
                  this.loading = true
                  saveDealHxsWfProtocol(this.form).then(response => {
                    if (response.code === 200) {
                      this.msgSuccess(this.$t('保存成功'))
                      this.loading = false
                      resolve(response.data.id)
                    } else {
                      this.msgError(response.msg)
                      this.loading = false
                      reject(response)
                    }
                  }).catch(() => {
                    this.loading = false
                  })
                } else {
                  this.locationError(document)
                  reject()
                }
              })
            })
          },
          // 查询聚焦行业二级字典
          changeTwo(option) {
            if (option) {
              this.form.industryFocusTwo = undefined
            }
            this.industryFocusTwoOptions = []
            const row = {
              parentId: this.form.industryFocus
            }
            industryList(row).then(response => {
              this.industryFocusTwoOptions = response.data
            })
          },
          // 删除团队成员
          delHandle(index) {
              this.form.dealWfHxsInvestmentList.splice(index, 1)
          },
          // 添加团队成员
          addHandle() {
            const row = {
              id: undefined,
              hostId: undefined,
              investorType: undefined,
            investorId: undefined,
            amountInvestmentEstimate: undefined
            }
            if (!(this.form.dealWfHxsInvestmentList instanceof Array)) {
              this.$set(this.form, 'dealWfHxsInvestmentList', [])
            }
            this.$nextTick(() => {
              this.form.dealWfHxsInvestmentList.push(row)
            })
          },
          // 验证主体名称是否存在
          isExist(value, index) {
            if (value) {
              this.form.dealWfHxsInvestmentList.forEach((column, ind) => {
                if (column.investorId === value && index !== ind) {
                  this.$message({
                    message: '投资主体不能重复',
                    type: 'warning'
                  })
                }
              })
            }
          }
        }
    }
</script>
